package com.software.chemistry.controller;

import com.software.chemistry.domain.Result;
import com.software.chemistry.entity.SearchHistory;
import com.software.chemistry.service.SearchHistoryService;
import com.software.chemistry.util.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/search_history")
public class SearchHistoryController {
    @Autowired
    private SearchHistoryService searchHistoryService;

    @RequestMapping(value = "/createSearchHistory",method = RequestMethod.POST )
    private Result<SearchHistory> createStudentInfo(@RequestParam("userId") String userId, @RequestParam("obtain") String obtain){
        long time = System.currentTimeMillis();
        java.sql.Timestamp timestamp = new java.sql.Timestamp(time);
        String history = timestamp.toString();
        SearchHistory searchHistory = new SearchHistory(userId,obtain,history);

        searchHistoryService.createStudentInfo(searchHistory);
        List<SearchHistory> searchHistories = searchHistoryService.findByUserIdOrderByHistoryTimeDesc(userId);
        return ResultUtil.success(searchHistories);
    }

    @GetMapping("/findByUserIdOrderByHistoryTimeDesc/{userId}")
    private Result<SearchHistory> findByUserIdOrderByHistoryTimeDesc(@PathVariable String userId){
        List<SearchHistory> searchHistories = searchHistoryService.findByUserIdOrderByHistoryTimeDesc(userId);


        return ResultUtil.success(searchHistories);
    }

    //全部
    @GetMapping("/findByUserId/{userId}")
    private Result<SearchHistory> findByUserId(@PathVariable String userId) {
        List<SearchHistory> searchHistories = searchHistoryService.findByUserId(userId);
        return ResultUtil.success(searchHistories);
    }

    @RequestMapping(value = "/deleteByUserId",method = RequestMethod.POST )
    private void deleteByUserId(@RequestParam("userId")String userId) {
//        List<SearchHistory> searchHistories = searchHistoryService.findByUserId(userId);
//        searchHistoryService.deleteAll(searchHistories);
        searchHistoryService.deleteByUserId(userId);

    }

}
